package persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import dto.LineBillDTO;

import model.LineBill;
import model.LineBillItem;

public class HistoricMapper {
	private static HistoricMapper s_HistoricMapper;

	private HistoricMapper() {
	}

	public static HistoricMapper GetInstancia() {
		if (s_HistoricMapper == null) {
			s_HistoricMapper = new HistoricMapper();
		}

		return s_HistoricMapper;
	}
	
	public ArrayList<LineBillDTO> getHistoricPerPeriod(Timestamp from, Timestamp to)
	{
		LineBillDTO lineBillDTO = null;
		 ArrayList<LineBillDTO> historicsBills = new  ArrayList<LineBillDTO>();
		//Obtener conexion BD 
		Connection con = PoolConnection.getPoolConnection().getConnection();
		
		//Generar sentencia SQL
		try
		{
			//Preparar sentencia insert en tabla destino
			String statement = "SELECT * from historycbills	where date >= ?	and date<= ? " ;
			PreparedStatement ps = null;
			ps = con.prepareStatement(statement);
			ps.setTimestamp(1,from);
			ps.setTimestamp(2,to);
			ResultSet result = ps.executeQuery();
			while (result.next())
			{
				lineBillDTO = new LineBillDTO( result.getString("userName"), 
						result.getString("LineNumber"),
						result.getString("details"), result.getString("details"), result.getDate("date")); 
				historicsBills.add(lineBillDTO);
			}
			
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
		  }
	      catch( SQLException e ) 
	      {
				System.out.println("Mensaje Error al buscar Historics: " + e.getMessage());
				System.out.println("Stack Trace al buscar Historics: " + e.getStackTrace());
				PoolConnection.getPoolConnection().realeaseConnection(con);
	      }
		finally
		{
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
      return historicsBills;
	}
	
	
}
